package com.cmz.common.utils;

import com.google.common.collect.Maps;
import com.cmz.common.enums.CommonError;
import com.cmz.common.exception.ServiceRuntimeException;

import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

/**
 * Properties 格式处理工具
 * @author cmz
 */
public class PropertiesUtils {

    private PropertiesUtils() {}

    public static Map<String, Object> readAsKeyVal(String content) {

        Properties properties = new Properties();
        try {
            properties.load(new StringReader(content));
        } catch (IOException e) {
            throw new ServiceRuntimeException(CommonError.XIOSDKCM0001, e);
        }

        Map<String, Object> result = Maps.newHashMap();
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            Object key = enumeration.nextElement();
            result.put(key.toString(), properties.get(key));
        }

        return result;
    }

}
